package cn.stard.org.service.impl;

import cn.stard.basic.util.MD5Utils;
import cn.stard.basic.util.StrUtils;
import cn.stard.org.query.EmployeeQuery;
import cn.stard.basic.util.PageList;
import cn.stard.org.domain.Employee;
import cn.stard.org.mapper.EmployeeMapper;
import cn.stard.org.service.IEmployeeservice;
import cn.stard.system.domain.Role;
import cn.stard.user.domain.Logininfo;
import cn.stard.user.domain.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeserviceImpl implements IEmployeeservice {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public List<Employee> loadAll() {
        return employeeMapper.loadAll();
    }

    @Override
    public Employee loadById(Long id) {

        return  employeeMapper.loadById(id);
    }

    @Override
    public void remove(Long id) {
        deleRelationTable(id);
        employeeMapper.remove(id);
    }

    public void deleRelationTable(Long id){
        /*因为他们的表是相关联,role的主键id是两张表的外键id所以这边关联删除主键id就相当于删除掉所以关联的表数据*/
        // 删除角色菜单表的数据
        employeeMapper.removeEmp(id);
    }

    @Override
    public void save(Employee employee) {
        // 密码是明文需要加密所以这边自定义了一个方法给进入到这边的加盐加密
        initEmployeerole(employee);
        // 把添加对象数据保存到登录对象里面
        Logininfo logininfo = employee2Logininfo(employee);
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.save(employee);
        // 初始化表
        initRelationTable(employee);
    }
        /*设置一个方法初始化关联表这里关联表初始的是员工*/
    public void initRelationTable(Employee employee){


        /*因为他们的表是相关联,role的主键id是两张表的外键id所以这边关联删除主键id就相当于删除掉所以关联的表数据*/
        // 删除角色菜单表的数据
        employeeMapper.removeEmployeerole(employee.getId());
        // 删除角色权限表数据
        employeeMapper.saveEmployeerole(employee.getId(),employee.getRoles());
    }
        // 初始化对象参数
    private  void initEmployeerole(Employee employee){
        // 获取一个盐值32位的
        String salt = StrUtils.getComplexRandomString(32);
        // 给盐值加入到salt里
        employee.setSalt(salt);
        // 根据盐值进行加密,设置到本对象了里
        String md5password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5password);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        // 设置用户登录的type为后台管理员然后返回给每次登录的对象
        logininfo.setType(0);
        return logininfo;
    }


    @Override
    public void update(Employee employee) {
        // 密码是明文需要加密所以这边自定义了一个方法给进入到这边的加盐加密
        initEmployeerole(employee);
        // 把添加对象数据保存到登录对象里面
        Logininfo logininfo = employee2Logininfo(employee);
        employee.setLogininfoId(logininfo.getId());
        initEmployeerole(employee);
        employeeMapper.update(employee);
        initRelationTable(employee);
    }


    @Override
    public PageList<Employee> queryData(EmployeeQuery employeeQuery) {

        /*高级查询数据*/
        List<Employee> rows = employeeMapper.queryData(employeeQuery);
        /*查询分页总数*/
        Integer total = employeeMapper.queryCount(employeeQuery);
        System.out.println(employeeQuery);
        return  new PageList<Employee>(total,rows);
    }

    @Override
    public Integer queryCount(Employee employeeQuery) {
        return null;
    }

    @Override
    public void deles(List<Long> ids) {
        employeeMapper.deles(ids);
    }

    @Override
    public Employee loadByloginfoId(Long id) {
        return employeeMapper.loadByloginfoId(id);
    }

    @Override
    public List<Employee> loadByShopId(Long shopId) {

        return employeeMapper.loadByShopId(shopId);
    }


}